package com.seu.vcampus.util;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.*;

/**
 * 界面样式工具类
 * 提供统一的现代化界面样式和组件美化功能
 * 
 * @author 虚拟校园开发团队
 * @since 1.0.0
 */
public class UIStyleUtil {
    
    // 主题颜色定义 - 调整为黑色文字
    public static final Color PRIMARY_COLOR = new Color(66, 133, 244);
    public static final Color SECONDARY_COLOR = new Color(108, 117, 125);
    public static final Color SUCCESS_COLOR = new Color(40, 167, 69);
    public static final Color WARNING_COLOR = new Color(255, 193, 7);
    public static final Color DANGER_COLOR = new Color(220, 53, 69);
    public static final Color LIGHT_COLOR = new Color(248, 249, 250);
    public static final Color DARK_COLOR = Color.BLACK; // 统一使用黑色文字
    
    // 背景颜色 - 使用更亮的背景
    public static final Color BACKGROUND_COLOR = Color.WHITE; // 改为白色背景
    public static final Color CARD_BACKGROUND = new Color(250, 250, 250); // 浅灰色卡片背景
    public static final Color GRADIENT_START = Color.WHITE;
    public static final Color GRADIENT_END = new Color(245, 245, 245);
    
    // 边框颜色
    public static final Color BORDER_COLOR = new Color(222, 226, 230);
    public static final Color INPUT_BORDER_COLOR = new Color(218, 220, 224);
    
    // 字体定义
    public static final Font TITLE_FONT = new Font("微软雅黑", Font.BOLD, 24);
    public static final Font SUBTITLE_FONT = new Font("微软雅黑", Font.BOLD, 18);
    public static final Font BODY_FONT = new Font("微软雅黑", Font.PLAIN, 14);
    public static final Font BUTTON_FONT = new Font("微软雅黑", Font.BOLD, 14);
    public static final Font SMALL_FONT = new Font("微软雅黑", Font.PLAIN, 12);
    
    /**
     * 创建现代化按钮
     */
    public static JButton createModernButton(String text, Color background, Color foreground) {
        JButton button = new JButton(text);
        button.setFont(BUTTON_FONT);
        button.setBackground(background);
        button.setForeground(foreground);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(background, 1),
            BorderFactory.createEmptyBorder(12, 24, 12, 24)
        ));
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        button.setOpaque(true);
        
        // 悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(background.brighter());
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(background);
            }
        });
        
        return button;
    }
    
    /**
     * 创建主色调按钮
     */
    public static JButton createPrimaryButton(String text) {
        return createModernButton(text, PRIMARY_COLOR, Color.BLACK);
    }
    
    /**
     * 创建成功按钮
     */
    public static JButton createSuccessButton(String text) {
        return createModernButton(text, SUCCESS_COLOR, Color.BLACK);
    }
    
    /**
     * 创建警告按钮
     */
    public static JButton createWarningButton(String text) {
        return createModernButton(text, WARNING_COLOR, Color.BLACK);
    }
    
    /**
     * 创建危险按钮
     */
    public static JButton createDangerButton(String text) {
        return createModernButton(text, DANGER_COLOR, Color.BLACK);
    }
    
    /**
     * 创建次要按钮
     */
    public static JButton createSecondaryButton(String text) {
        return createModernButton(text, SECONDARY_COLOR, Color.BLACK);
    }
    
    /**
     * 创建现代化输入框
     */
    public static JTextField createModernTextField(String placeholder) {
        JTextField textField = new JTextField(20);
        textField.setFont(BODY_FONT);
        textField.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(INPUT_BORDER_COLOR, 1),
            BorderFactory.createEmptyBorder(12, 16, 12, 16)
        ));
        
        // 设置占位符
        textField.putClientProperty("JTextField.placeholderText", placeholder);
        
        // 悬停效果
        textField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                textField.setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(PRIMARY_COLOR, 1),
                    BorderFactory.createEmptyBorder(12, 16, 12, 16)
                ));
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                textField.setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(INPUT_BORDER_COLOR, 1),
                    BorderFactory.createEmptyBorder(12, 16, 12, 16)
                ));
            }
        });
        
        return textField;
    }
    
    /**
     * 创建现代化密码框
     */
    public static JPasswordField createModernPasswordField(String placeholder) {
        JPasswordField passwordField = new JPasswordField(20);
        passwordField.setFont(BODY_FONT);
        passwordField.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(INPUT_BORDER_COLOR, 1),
            BorderFactory.createEmptyBorder(12, 16, 12, 16)
        ));
        
        // 设置占位符
        passwordField.putClientProperty("JTextField.placeholderText", placeholder);
        
        // 悬停效果
        passwordField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                passwordField.setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(PRIMARY_COLOR, 1),
                    BorderFactory.createEmptyBorder(12, 16, 12, 16)
                ));
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                passwordField.setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(INPUT_BORDER_COLOR, 1),
                    BorderFactory.createEmptyBorder(12, 16, 12, 16)
                ));
            }
        });
        
        return passwordField;
    }
    
    /**
     * 创建现代化标签
     */
    public static JLabel createModernLabel(String text, Font font, Color color) {
        JLabel label = new JLabel(text);
        label.setFont(font);
        label.setForeground(color);
        return label;
    }
    
    /**
     * 创建标题标签
     */
    public static JLabel createTitleLabel(String text) {
        return createModernLabel(text, TITLE_FONT, PRIMARY_COLOR);
    }
    
    /**
     * 创建副标题标签
     */
    public static JLabel createSubtitleLabel(String text) {
        return createModernLabel(text, SUBTITLE_FONT, DARK_COLOR);
    }
    
    /**
     * 创建卡片式面板
     */
    public static JPanel createCardPanel() {
        JPanel panel = new JPanel();
        panel.setBackground(CARD_BACKGROUND);
        panel.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(BORDER_COLOR, 1),
            BorderFactory.createEmptyBorder(20, 20, 20, 20)
        ));
        return panel;
    }
    
    /**
     * 创建带标题的卡片面板
     */
    public static JPanel createCardPanel(String title) {
        JPanel panel = createCardPanel();
        panel.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(BORDER_COLOR, 1),
                title,
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
                javax.swing.border.TitledBorder.DEFAULT_POSITION,
                SUBTITLE_FONT,
                PRIMARY_COLOR
            ),
            BorderFactory.createEmptyBorder(15, 15, 15, 15)
        ));
        return panel;
    }
    
    /**
     * 创建渐变背景面板
     */
    public static JPanel createGradientPanel() {
        return new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                GradientPaint gradient = new GradientPaint(0, 0, GRADIENT_START, 
                                                      getWidth(), getHeight(), GRADIENT_END);
                g2d.setPaint(gradient);
                g2d.fillRect(0, 0, getWidth(), getHeight());
            }
        };
    }
    
    /**
     * 创建现代化滚动面板
     */
    public static JScrollPane createModernScrollPane(Component view) {
        JScrollPane scrollPane = new JScrollPane(view);
        scrollPane.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5),
            BorderFactory.createLineBorder(BORDER_COLOR, 1)
        ));
        scrollPane.getVerticalScrollBar().setUI(new javax.swing.plaf.basic.BasicScrollBarUI() {
            @Override
            protected void configureScrollBarColors() {
                this.thumbColor = PRIMARY_COLOR;
                this.trackColor = LIGHT_COLOR;
            }
        });
        return scrollPane;
    }
    
    /**
     * 设置全局样式
     */
    public static void setupGlobalStyle() {
        try {
            // 设置系统外观
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            
            // 设置标签页样式
            UIManager.put("TabbedPane.selected", PRIMARY_COLOR);
            UIManager.put("TabbedPane.background", LIGHT_COLOR);
            UIManager.put("TabbedPane.foreground", DARK_COLOR);
            UIManager.put("TabbedPane.font", BODY_FONT);
            
            // 设置按钮样式
            UIManager.put("Button.font", BUTTON_FONT);
            UIManager.put("Button.background", PRIMARY_COLOR);
            UIManager.put("Button.foreground", Color.WHITE);
            
            // 设置表格样式
            UIManager.put("Table.font", BODY_FONT);
            UIManager.put("TableHeader.font", new Font("微软雅黑", Font.BOLD, 14));
            UIManager.put("TableHeader.background", LIGHT_COLOR);
            UIManager.put("TableHeader.foreground", DARK_COLOR);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 显示现代化消息对话框
     */
    public static void showModernMessageDialog(Component parent, String message, String title, int messageType) {
        Color backgroundColor = CARD_BACKGROUND;
        Color foregroundColor = DARK_COLOR;
        
        switch (messageType) {
            case JOptionPane.ERROR_MESSAGE:
                backgroundColor = DANGER_COLOR;
                foregroundColor = Color.WHITE;
                break;
            case JOptionPane.WARNING_MESSAGE:
                backgroundColor = WARNING_COLOR;
                foregroundColor = Color.BLACK;
                break;
            case JOptionPane.INFORMATION_MESSAGE:
                backgroundColor = PRIMARY_COLOR;
                foregroundColor = Color.WHITE;
                break;
        }
        
        JOptionPane.showMessageDialog(parent, message, title, messageType);
    }
    
    /**
     * 显示现代化确认对话框
     */
    public static int showModernConfirmDialog(Component parent, String message, String title) {
        return JOptionPane.showConfirmDialog(parent, message, title, 
                                           JOptionPane.YES_NO_OPTION, 
                                           JOptionPane.QUESTION_MESSAGE);
    }
    
    /**
     * 创建只读文本框
     */
    public static JTextField createReadOnlyTextField() {
        JTextField textField = new JTextField(20);
        textField.setFont(BODY_FONT);
        textField.setEditable(false);
        textField.setBackground(LIGHT_COLOR);
        textField.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(BORDER_COLOR, 1),
            BorderFactory.createEmptyBorder(12, 16, 12, 16)
        ));
        return textField;
    }
}